Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports Vintasoft.Imaging
Imports DemosCommonCode.Imaging.Codecs
Imports Vintasoft.Imaging.Codecs

Namespace Imaging
	Public Class SaveImageFileForm

		#Region "Methods"

		Public Shared Function SaveImageToFile(image As VintasoftImage) As DialogResult
			If image Is Nothing Then
				Return DialogResult.OK
			End If

			Dim saveFileDlg As New SaveFileDialog()
			CodecsFileFilters.SetFilters(saveFileDlg, False)
			If saveFileDlg.ShowDialog() <> DialogResult.OK Then
				Return DialogResult.Cancel
			End If
			Dim encoder As EncoderBase = Nothing
			If EncoderFactory.GetEncoder(saveFileDlg.FileName, encoder) Then
				Try
					image.Save(saveFileDlg.FileName, encoder)
				Catch e As Exception
					MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
				End Try
				Return DialogResult.OK
			End If
			Return DialogResult.Cancel
		End Function

		Public Shared Function SaveImagesToFile(images As ImageCollection) As DialogResult
			If images.Count = 0 Then
				Return DialogResult.OK
			End If
			If images.Count = 1 Then
				Return SaveImageToFile(images(0))
			End If

			Dim saveFileDlg As New SaveFileDialog()
			CodecsFileFilters.SetFilters(saveFileDlg, True)
			If saveFileDlg.ShowDialog() <> DialogResult.OK Then
				Return DialogResult.Cancel
			End If
			Dim encoder As MultipageEncoderBase = Nothing
			If EncoderFactory.GetMultipageEncoder(saveFileDlg.FileName, encoder) Then
				Try
					images.SaveSync(saveFileDlg.FileName, encoder)
				Catch e As Exception
					MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
				End Try
				Return DialogResult.OK
			End If
			Return DialogResult.Cancel
		End Function

		#End Region

	End Class
End Namespace
